// 静态路由规则  动态路由规则
import { constantRoutes, asyncRoutes } from '@/router/index'

const state = {
  routes: []
}
const mutations = {
  setRoutes(state, data) {
    //  3. 动态路由规则和静态路由规则拼接 用于渲染左侧菜单
    state.routes = [...constantRoutes, ...data]
  }
}
const actions = {
  filterRoutes(store, data) {
    // console.log(asyncRoutes)
    // console.log(data)
    // 1.根据用户的权限 过滤出能访问的动态路由规则
    const routes = asyncRoutes.filter(item => data.indexOf(item.name) !== -1)
    // console.log(routes)
    //  2.2 把能访问的动态路由规则传给函数
    store.commit('setRoutes', routes)
    // 2.1 返回出去 用于动态添加路由规则
    return routes
  }
}
export default {
  // 命名空间
  namespaced: true,
  state,
  mutations,
  actions
}
